//
//  UIBarButtonItem+Extension.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/11.
//  Copyright © 2015年 cz. All rights reserved.
//

import Foundation
import UIKit

extension UIBarButtonItem{
    class func item(imageName: String = "", title: String = "", target: AnyObject?, action: Selector) -> UIBarButtonItem{
        //初始化一个btn
        let button = UIButton()
        //设置button属性
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        //判断是否有title
        if title.characters.count > 0 {
            //设置文字属性
            button.setTitle(title, forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            //设置文字不同状态下字体颜色
            button.setTitleColor(RGB(r: 80,g: 80,b: 80), forState: UIControlState.Normal)
            
            button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        }
        if imageName.characters.count > 0 {
        //设置btn图片
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
        button.sizeToFit()
        //返回
        return UIBarButtonItem(customView: button)
    }
}
